package com.platform.car.socket;

import java.io.IOException;
import java.net.Socket;

/**
 * 车锁socket处理线程
 * Created by GaoXiang
 */
public class SocketTask implements Runnable {

    private Socket socket;

    private String number;

    private String power;

    public SocketTask(){};

    public SocketTask(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            System.out.println("新连接:"+socket.getInetAddress()+":"+socket.getPort());
            handleSocket();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 跟客户端Socket进行通信
     * @throws Exception
     */
    private void handleSocket() throws Exception {

        //开始读取socket信息
        while (true) {
            String message = "";
            try {
                //获取传输过来的值
                byte[] content = new byte[256];
                int count = socket.getInputStream().read(content);
                System.out.println("接收到信息，长度：" + count);
                message = new String(content,0,count);
                //打印收到的消息
                //showMessage(message);
            } catch (IOException e) {
                e.printStackTrace();
            }

            //客户端断开时会不停的出现空，此时终止此线程
            if(message.trim().equals("")){
                SocketUtil.socketTaskMap.remove(number);
                break;
            }
            String[] commend = message.split(",");
            switch (commend[2]){
                case "1" : {
                    number = commend[1];
                    power = commend[4];
                    System.out.println("设备注册,设备号：" + number + "；电量：" + power);
                    //注册到内存
                    SocketUtil.socketTaskMap.put(number,this);
                } break;
                case "3" : {
                    String type = commend[3];
                    if(type.equals("1")){
                        System.out.println("设备开锁,设备号：" + number);
                    }
                    if(type.equals("2")){
                        System.out.println("设备关锁,设备号：" + number);
                    }
                } break;
                default:{} break;
            }

            //处理心跳信息
            if(message.contains(",")){

                continue;
            }

        }
    }

    /**
     * 开锁
     */
    public void openLock(){
        String commend = "hrbc17," + number + ",3,1,71cbrh" ;
        try {
            socket.getOutputStream().write(commend.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("设备：" + number + "开锁失败");
        }
    }

    /**
     * 关锁
     */
    public void closeLock(){
        String commend = "hrbc17," + number + ",3,2,71cbrh" ;
        try {
            socket.getOutputStream().write(commend.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("设备：" + number + "关锁失败");
        }
    }

    /**
     * 索要状态
     */
    public void getLockStatus(){
        String commend = "hrbc17," + number + ",4,71cbrh" ;
        try {
            socket.getOutputStream().write(commend.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("设备：" + number + "关锁失败");
        }
    }

    /**
     * 预约
     */
    public void yuyue(){
        String commend = "hrbc17," + number + ",4,71cbrh" ;
        try {
            socket.getOutputStream().write(commend.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("设备：" + number + "关锁失败");
        }
    }

    private void showMessage(String message){
        System.out.println(message);
    }

    public Socket getSocket() {
        return socket;
    }

    public void setSocket(Socket socket) {
        this.socket = socket;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getPower() {
        return power;
    }

    public void setPower(String power) {
        this.power = power;
    }
}
